<?php
namespace Home\Controller;
use Think\Controller;

header("content-Type: text/html; charset=Utf-8");

class MessageController extends Controller {
    public function index(){
    	echo "Message";
	}


	public function getAll(){
		$this_device_ID = I('this_device_ID');
		$db_message = M('Message');
		$db_Circle = M('Circle');
		$db_DeviceToCircle = M('DeviceToCircle');
		$circles = $db_DeviceToCircle->where("Device_ID = '%s'", $this_device_ID)->order("DeviceToCircle_StartDateTime desc")->select();
		$Local = $circles[0];
		$Local_Circle_ID = $Local['Circle_ID'];

		$send = $db_message	->where("From_Circle_ID = '%d'", $Local_Circle_ID)
							->join("left join Circle as From_Circle on Message.From_Circle_ID = From_Circle.Circle_ID")
							->join("left join Circle as To_Circle on Message.To_Circle_ID= To_Circle.Circle_ID")
							->field("Message_ID, From_Circle.Circle_Name as From_Circle_Name, To_Circle.Circle_Name as To_Circle_Name, Message_description, Message_pubDate, Message.From_Circle_ID, Message.To_Circle_ID")
							->select();
		
		$receive = $db_message	->where("To_Circle_ID = '%d'", $Local_Circle_ID)
								->join("left join Circle as From_Circle on Message.From_Circle_ID = From_Circle.Circle_ID")
								->join("left join Circle as To_Circle on Message.To_Circle_ID= To_Circle.Circle_ID")
								->field("Message_ID, From_Circle.Circle_Name as From_Circle_Name, To_Circle.Circle_Name as To_Circle_Name, Message_description, Message_pubDate, Message.From_Circle_ID, Message.To_Circle_ID")
								->select();

		$msgs = array_merge($send, $receive);


		$msg_id = array();

		foreach($msgs as $msg){
			$msg_id[] = $msg['Message_ID'];
		}

		array_multisort($msg_id, SORT_DESC, $msgs);


		$res = array();
		$ids = array();

		array_push($ids, $Local_Circle_ID);

		foreach($msgs as $msg){
			if(!in_array($msg['From_Circle_ID'], $ids)){
				if($msg['From_Circle_ID']==$Local_Circle_ID){
					$msg['From_Circle_Name'] = $msg['To_Circle_Name'];
				}
				array_push($res, $msg);
				array_push($ids, $msg['From_Circle_ID']);
			}

			if(!in_array($msg['To_Circle_ID'], $ids)){
				if($msg['From_Circle_ID']==$Local_Circle_ID){
					$msg['From_Circle_Name'] = $msg['To_Circle_Name'];
				}
				array_push($res, $msg);
				array_push($ids, $msg['To_Circle_ID']);
			}
		}

		//$array = array("local cicle"=> $Local_Circle_ID, "Remote"=> $Remote_Circle_ID);
		
		echo json_encode($res);


	}


	public function getMessage(){
		$chat_room = I('chat_room');
		$this_device_ID = I('this_device_ID');

		$db_message = M('Message');
		$db_Circle = M('Circle');
		$db_DeviceToCircle = M('DeviceToCircle');
		
		$circles = $db_DeviceToCircle->where("Device_ID = '%s'", $this_device_ID)->order("DeviceToCircle_StartDateTime desc")->select();
		$Local = $circles[0];
		$Local_Circle_ID = $Local['Circle_ID'];

		$Remote = $db_Circle->where("Circle_Name = '%s'", $chat_room)->find();
		$Remote_Circle_ID = $Remote['Circle_ID'];


		$send = $db_message	->where("From_Circle_ID = '%d' and To_Circle_ID = '%d'", $Local_Circle_ID, $Remote_Circle_ID)
							->join("left join Circle as From_Circle on Message.From_Circle_ID = From_Circle.Circle_ID")
							->join("left join Circle as To_Circle on Message.To_Circle_ID= To_Circle.Circle_ID")
							->field("Message_ID, From_Circle.Circle_Name as From_Circle_Name, To_Circle.Circle_Name as To_Circle_Name, Message_description, Message_pubDate, Message.From_Circle_ID, Message.To_Circle_ID")
							->select();
		
		$receive = $db_message	->where("From_Circle_ID = '%d' and To_Circle_ID = '%d'", $Remote_Circle_ID, $Local_Circle_ID)
								->join("left join Circle as From_Circle on Message.From_Circle_ID = From_Circle.Circle_ID")
								->join("left join Circle as To_Circle on Message.To_Circle_ID= To_Circle.Circle_ID")
								->field("Message_ID, From_Circle.Circle_Name as From_Circle_Name, To_Circle.Circle_Name as To_Circle_Name, Message_description, Message_pubDate, Message.From_Circle_ID, Message.To_Circle_ID")
								->select();

		$msgs = array_merge($send, $receive);

		$msg_id = array();

		foreach($msgs as $msg){
			$msg_id[] = $msg['Message_ID'];
		}

		array_multisort($msg_id, SORT_ASC, $msgs);

		//$array = array("local cicle"=> $Local_Circle_ID, "Remote"=> $Remote_Circle_ID);

		$ids = array();

		$res = array();

		foreach($msgs as $msg){
			if(!in_array($msg['Message_ID'], $ids)){
				array_push($res, $msg);
				array_push($ids, $msg['Message_ID']);
			}

			if(!in_array($msg['Message_ID'], $ids)){
				array_push($res, $msg);
				array_push($ids, $msg['Message_ID']);
			}
		}
		
		echo json_encode($res);
	}

	public function postMessage(){
		$chat_room = I('chat_room');
		$this_device_ID = I('this_device_ID');
		$msg = I('msg');

		$db_message = M('Message');
		$db_Circle = M('Circle');
		$db_DeviceToCircle = M('DeviceToCircle');

		$to_Circle = $db_Circle->where("Circle_Name = '%s'", $chat_room)->find();
		$To_Circle_ID = $to_Circle['Circle_ID'];

		$circles = $db_DeviceToCircle->where("Device_ID = '%s'", $this_device_ID)->order("DeviceToCircle_StartDateTime desc")->select();
		$from_Circle = $circles[0];
		$From_Circle_ID = $from_Circle['Circle_ID'];

		$data['Message_description'] = $msg;
		$data['From_Circle_ID'] = $From_Circle_ID;
		$data['To_Circle_ID'] = $To_Circle_ID;

		$res = $db_message->add($data);
		if($res){
			$array = array('success'=>1);
			echo json_encode($array);
		}else{
			$array = array('success'=>0, 'error'=>"发送失败");
			echo json_encode($array);
		}
	}


	public function welcome(){
		$To_Circle_ID = I('To_Circle_ID');
		$From_Circle_ID = I('From_Circle_ID');

		$db_message = M('Message');
		$db_welcome = M('welcome');

		$welcome = $db_welcome->where("id=1")->find();
		$msg = $welcome['message'];

		$data['Message_description'] = $msg;
		$data['From_Circle_ID'] = $From_Circle_ID;
		$data['To_Circle_ID'] = $To_Circle_ID;

		$res = $db_message->add($data);
		if($res){
			$array = array('success'=>1);
			echo json_encode($array);
		}else{
			$array = array('success'=>0, 'error'=>"发送失败");
			echo json_decode($array);
		}
	}
}